package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.companyconfig.pojo.dto.CompanyConfigGetPriceConfigDTO;
import com.xbongbong.pro.companyconfig.pojo.vo.CompanyConfigGetPriceConfigVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.priceset.pojo.dto.PriceProductSetDTO;
import com.xbongbong.pro.priceset.pojo.dto.PriceProductSetDelDTO;
import com.xbongbong.pro.priceset.pojo.dto.PriceProductSetSaveDTO;
import com.xbongbong.pro.priceset.pojo.dto.PriceSetConditionDTO;
import com.xbongbong.pro.priceset.pojo.dto.PriceSetGetDTO;
import com.xbongbong.pro.priceset.pojo.dto.PriceSetListDTO;
import com.xbongbong.pro.priceset.pojo.dto.PriceSetSaveDTO;
import com.xbongbong.pro.priceset.pojo.dto.PriceSetSortDTO;
import com.xbongbong.pro.priceset.pojo.vo.PriceProductSetListVO;
import com.xbongbong.pro.priceset.pojo.vo.PriceProductSetSaveVO;
import com.xbongbong.pro.priceset.pojo.vo.PriceSetFormTemplateVO;
import com.xbongbong.pro.priceset.pojo.vo.PriceSetGetConditionVO;
import com.xbongbong.pro.priceset.pojo.vo.PriceSetGetVO;
import com.xbongbong.pro.priceset.pojo.vo.PriceSetSaveVO;
import com.xbongbong.saas.service.PriceSetService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/priceSet")
public class PriceSetController {

    @Resource
    private PriceSetService priceSetService;

    /**
     * 价目表列表
     *
     * @param priceSetListDTO 入参
     * @param br
     * @return
     */
    @RequestMapping(value = "/getPriceSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getPriceSet(@RequestBody @Valid PriceSetListDTO priceSetListDTO, BindingResult br) {
        XbbResponse<CompanyConfigGetPriceConfigVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompanyConfigGetPriceConfigVO companyConfigGetPriceConfigVO = priceSetService.getPriceSet(priceSetListDTO);
                response = new XbbResponse<>(companyConfigGetPriceConfigVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response);
    }

    /**
     * 启用或关闭价目表
     *
     * @return
     */
    @RequestMapping(value = "/priceEnableOrClose", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String priceEnableOrClose(@RequestBody @Valid CompanyConfigGetPriceConfigDTO companyConfigGetPriceConfigDTO, BindingResult br) {
        XbbResponse<PriceSetGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PriceSetGetVO priceSetGetVO = priceSetService.priceEnableOrClose(companyConfigGetPriceConfigDTO);
                response = new XbbResponse<>(priceSetGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除价目表
     *
     * @param companyConfigGetPriceConfigDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/delPriceSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delPriceSet(@RequestBody @Valid CompanyConfigGetPriceConfigDTO companyConfigGetPriceConfigDTO, BindingResult br) {
        XbbResponse<PriceSetGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PriceSetGetVO priceSetGetVO = priceSetService.delPriceSet(companyConfigGetPriceConfigDTO);
                response = new XbbResponse<>(priceSetGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/template", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String template(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<PriceSetFormTemplateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PriceSetFormTemplateVO priceSetFormTemplateVO = priceSetService.getTemplateForm(baseDTO);
                response = new XbbResponse<>(priceSetFormTemplateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取筛选条件
     *
     * @param priceSetConditionDTO 入参
     * @param br
     * @return
     * @throws
     * @author hongxiao
     * @date: 2019-03-03 15:18
     * @version v1.0
     * @since v1.0
     */
    @RequestMapping(value = "/getCondition", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getCondition(@RequestBody @Valid PriceSetConditionDTO priceSetConditionDTO, BindingResult br) {
        XbbResponse<PriceSetGetConditionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PriceSetGetConditionVO priceSetGetConditionVO = priceSetService.getCondition(priceSetConditionDTO);
                response = new XbbResponse<>(priceSetGetConditionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 保存价目表
     *
     * @param priceSetSaveDTO 入参
     * @param br              参数绑定
     * @return
     */
    @RequestMapping(value = "/savePriceSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String savePriceSet(@RequestBody @Valid PriceSetSaveDTO priceSetSaveDTO, BindingResult br) {
        XbbResponse<PriceSetSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(priceSetSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
//                    if (!paasRedisHelper.checkConcurrentLock(priceSetSaveDTO)) {
//                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
//                        return JSON.toJSONString(response);
//                    }
                }
                PriceSetSaveVO priceSetSaveVO = priceSetService.savePriceSet(priceSetSaveDTO);
                response = new XbbResponse<>(priceSetSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 价目表产品保存
     *
     * @param priceProductSetSaveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/savePriceProductSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String savePriceProductSet(@RequestBody @Valid PriceProductSetSaveDTO priceProductSetSaveDTO, BindingResult br) {
        XbbResponse<PriceProductSetSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(priceProductSetSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
//                    if (!paasRedisHelper.checkConcurrentLock(priceSetSaveDTO)) {
//                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
//                        return JSON.toJSONString(response);
//                    }
                }
                PriceProductSetSaveVO priceProductSetSaveVO = priceSetService.savePriceProductSet(priceProductSetSaveDTO);
                response = new XbbResponse<>(priceProductSetSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 编辑价目表
     *
     * @param priceSetGetDTO 入参
     * @param br
     * @return
     */
    @RequestMapping(value = "/editPriceSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getPriceSet(@RequestBody @Valid PriceSetGetDTO priceSetGetDTO, BindingResult br) {
        XbbResponse<PriceSetGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PriceSetGetVO priceSetGetVO = priceSetService.editPriceSet(priceSetGetDTO);
                response = new XbbResponse<>(priceSetGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 价目表产品列表
     *
     * @param priceProductSetDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/getPriceProductSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getPriceProductSet(@RequestBody @Valid PriceProductSetDTO priceProductSetDTO, BindingResult br) {
        XbbResponse<PriceProductSetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PriceProductSetListVO priceProductSetVO = priceSetService.getPriceProductSet(priceProductSetDTO);
                response = new XbbResponse<>(priceProductSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @param priceSetSortDTO 入参
     * @param br              参数绑定
     * @return 结果
     */
    @RequestMapping(value = "/sort/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sort(@RequestBody @Valid PriceSetSortDTO priceSetSortDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = priceSetService.sort(priceSetSortDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @param priceProductSetDTO 入参
     * @param br                 参数绑定
     * @return 结果
     */
    @RequestMapping(value = "/getLinkProductField", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getLinkProductField(@RequestBody @Valid PriceProductSetDTO priceProductSetDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = priceSetService.getLinkProductField(priceProductSetDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 删除产品
     *
     * @param priceProductSetDelDTO 入参
     * @param br                    参数绑定
     * @return 结果
     */
    @RequestMapping(value = "/deleteProductPrice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteProductPrice(@RequestBody @Valid PriceProductSetDelDTO priceProductSetDelDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = priceSetService.deleteProductPrice(priceProductSetDelDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
